'''
https://leetcode.cn/problems/magical-string/description/
'''


class Solution:
    def magicalString(self, n: int) -> int:
        magic = []
        flag = True
        i = 0
        while len(magic) < n:
            if flag:
                magic.append(1)
                if magic[i] == 2:
                    magic.append(1)
            else:
                magic.append(2)
                if magic[i] == 2:
                    magic.append(2)
            i += 1
            flag = not flag
        return sum(num for num in magic[0:n] if num == 1)

n = 4
print(Solution().magicalString(n))

# 1221121221221121122
# 1221121221221121122121121221121121221221121221211211221221121221221121121221211221221121221221121122
